import {createSlice} from "@reduxjs/toolkit";
import axios from "axios";
import {data} from "react-router-dom";


const billStore=createSlice({
    name:'bill',
    initialState:{
        billList:[]
    },
    reducers:{
        // 同步修改方法
        setBillList(state,action){
            state.billList=action.payload;
        },
    //     同步添加账单方法
        addBill(state,action){
            state.billList=action.payload;
        }
    }
})

//解构actionCreater函数
const {setBillList,addBill}=billStore.actions;

// 编写异步函数
const getBillList=()=>{
    return async (dispatch)=>{
        const res=await axios.get('http://localhost:8888/ka');
        dispatch(setBillList(res.data))
    }
}

const addBillList=(data)=>{
    return async (dispatch)=>{
        const res=await axios.post('http://localhost:8888/ka',data);
        dispatch(addBill(res.data))
    }

}


//或者reducer函数对象
const billReducer=billStore.reducer;

export {getBillList,addBillList}


export default billReducer;